<?php

namespace app\admin\controller;
use app\common\service\BalanceService;
use think\Db;

//提现管理
class Withdrawal extends Common{

	public function index(){
		return $this->fetch();
	}


	public function dataList(){
		$limit = request()->param('limit')?:20;
		$username   = request()->param('username');
		$status   = request()->param('status');
		$coin_name   = request()->param('coin_name');
		$start_time = request()->param('start_time');
		$end_time   = request()->param('end_time');
		$uid   = request()->param('uid');
		$where      = '1=1';
		if ($username != '') {
            $where .= ' and u.username like \'%' . $username . '%\'';
        }
        if($status != '') {
            $where .= ' and t.status='.$status;
        }
        if($uid != '') {
            $where .= ' and t.uid='.$uid;
        }
        if ($coin_name != '') {
            $where .= ' and c.name like \'%' . $coin_name . '%\'';
        }
        if($start_time != '') {
            $where .= ' and t.date>="'.$start_time.'"';
        }
		if($end_time != '') {
            $where .= ' and t.date<="'.$end_time.'"';
        }
		$data  = Db::table('tp_winthdrawal_record')
				->alias('t')
				->join('tp_user u','u.id = t.uid','LEFT')
				->join('tp_coin c','c.id = t.cid','LEFT')
				->where($where)
				->order('t.id desc')
				->field('t.*,u.username,c.name coin_name')
				->paginate($limit);
		$data = $data->toArray();
		if(empty($data['data'])){
			$data['data']  = Db::table('tp_winthdrawal_record')
				->alias('t')
				->join('tp_user u','u.id = t.uid','LEFT')
				->join('tp_coin c','c.id = t.cid','LEFT')
				->where($where)
				->order('t.id desc')
				->field('t.*,u.username,c.name coin_name')
				->page(1)
				->limit($limit)
				->select();		
		}
		return $this->tableData($data);
	}

	public function excel(){
		$limit = request()->param('limit')?:20;
		$username   = request()->param('username');
		$status   = request()->param('status');
		$coin_name   = request()->param('coin_name');
		$start_time = request()->param('start_time');
		$end_time   = request()->param('end_time');
		$uid   = request()->param('uid');
		$where      = '1=1';
		if ($username != '') {
            $where .= ' and u.username like \'%' . $username . '%\'';
        }
        if($status != '') {
            $where .= ' and t.status='.$status;
        }
        if($uid != '') {
            $where .= ' and t.uid='.$uid;
        }
        if ($coin_name != '') {
            $where .= ' and c.name like \'%' . $coin_name . '%\'';
        }
         if($start_time != '') {
            $where .= ' and t.date>="'.$start_time.'"';
        }
		if($end_time != '') {
            $where .= ' and t.date<="'.$end_time.'"';
        }
		$data  = Db::table('tp_winthdrawal_record')
				->alias('t')
				->join('tp_user u','u.id = t.uid','LEFT')
				->join('tp_coin c','c.id = t.cid','LEFT')
				->where($where)
				->order('t.id desc')
				->field('t.uid,u.username,c.name coin_name,t.t_amount,t.fee,t.amount,t.bank_name,t.bank_user,t.bank_card,t.status,t.date')
				->select();
		foreach ($data as $key => $val) {
			$data[$key]['username'] = ' '.$data[$key]['username'];
			$data[$key]['bank_card'] = ' '.$data[$key]['bank_card'];
		}
		return exportExcel(['用户编号','用户名','提现钱包','总提现数量','扣除手续费','剩余数量','提现银行','开户人','银行账号','状态(1=通过，0=待审核，-1=已拒绝)','提现时间'], $data);
	}

	/*
	提现审核
	 */
	public function pass(){
		if (request()->method() == 'POST') {
			$data = [];
			$id   = intval(request()->param('id'));
			$remark = request()->param('value');
			$info = Db::table('tp_winthdrawal_record')->where('id',$id)->find();
			if($info['status'] != 0){
				return ['code'=>1,'msg'=>'订单状态错误'];
			}
			$res  = Db::table('tp_winthdrawal_record')->where('id',$id)->update([
					'status'=>1,
					'remark' => '已通过',
					'check_remark'=>$remark,
					'check_date'=>date('Y-m-d H:i:s')
				]);
			if ($res) {
				return ['code'=>0,'msg'=>'操作成功'];
			} else {
				return ['code'=>1,'msg'=>'操作失败'];
			}			
		}
	}

	/*
	提现拒绝
	 */
	public function refuse(){
		if (request()->method() == 'POST') {
			$data = [];
			$id   = intval(request()->param('id'));
			$remark = request()->param('value');
			$info = Db::table('tp_winthdrawal_record')->where('id',$id)->find();
			if($info['status'] != 0){
				return ['code'=>1,'msg'=>'订单状态错误'];
			}
			Db::startTrans();
			//退回资产
			$res1 = BalanceService::updateBalance($info['uid'],$info['cid'],abs(-$info['amount']),'提现驳回',33,$id);
			$res2 = Db::table('tp_winthdrawal_record')->where('id',$id)->update([
				'status'=>-1,
				'remark' => '已拒绝',
				'check_remark'=>$remark,
				'check_date'=>date('Y-m-d H:i:s')
			]);
			if ($res1 && $res2) {
				Db::commit();
				return ['code'=>0,'msg'=>'success'];
			} else {
				Db::rollback();
				return ['code'=>1,'msg'=>'更新失败'];
			}
		}
	}

}